package com.zx.controller;


import com.zx.bean.BootStrapTableExpress;
import com.zx.bean.Express;
import com.zx.bean.Message;

import com.zx.bean.ResultData;
import com.zx.mvc.ResponseBody;
import com.zx.service.ExpressService;
import com.zx.util.DateFormatUtil;
import com.zx.util.JsonUtil;
import com.zx.util.UserUtil;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;



public class ExpressController {
    @ResponseBody("/express/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Integer>> data = ExpressService.console();
        Message msg = new Message();

        if (data.size() == 0) {
            msg.setStatus(-1);
        } else {
            msg.setStatus(0);

        }
        msg.setData(data);
        String json = JsonUtil.toJson(msg);
        return json;  //return 出去


    }

    @ResponseBody("/express/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response) {
        //1 获取起始索引值
        int offset = Integer.parseInt(request.getParameter("offset"));
        //2 当前页 多少数量
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //3进行查询
        List<Express> list = ExpressService.findAll(true, offset, pageNumber);
        //4 将集合封装为bootstrap识别的格式  比如时间  状态
        //创建一个返回给前端bootstrap识别集合
        List<BootStrapTableExpress> list2 = new ArrayList<>();
        for (Express e : list) {
            String intime = DateFormatUtil.format(e.getIntime());
            String outtime = e.getOuttime() == null ? "未治疗" : DateFormatUtil.format(e.getOuttime());
            String status = e.getStatus() == 0 ? "待治疗" : "已治疗";
            String code = e.getCode() == null ? "已治疗" : e.getCode();
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(), e.getNumber(), e.getUsername(), e.getUserphone(), e.getCompany(), code, intime, outtime, status, e.getSysPhone());
            list2.add(e2);
        }
        //获取快递的总数
        List<Map<String, Integer>> console = ExpressService.console();
        int total = console.get(0).get("data1_size");
        //4 将集合封装为bootstap-table 识别的格式
        ResultData<BootStrapTableExpress> data = new ResultData<>();
        data.setRows(list2);

        data.setTotal(total);
        String json = JsonUtil.toJson(data);
        return json;


    }


    @ResponseBody("/express/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response) {
        String number = request.getParameter("number");
        String username = request.getParameter("username");
        String userphone = request.getParameter("userPhone");
        String company = request.getParameter("company");

        Express express = new Express(number, username, userphone, company, UserUtil.getUserName(request.getSession()));

        Message msg = new Message();
        boolean flag = ExpressService.insert(express);
        if (flag) {
            msg.setStatus(0);
            msg.setResult("病人信息录入成功");
        } else {
            msg.setStatus(-1);
            msg.setResult("病人信息录入失败");

        }

        String s = JsonUtil.toJson(msg);
        return s;


    }

    @ResponseBody("/express/find.do")
    public String find(HttpServletRequest request, HttpServletResponse response) {
        String number = request.getParameter("number");
        Express byNumber = ExpressService.findByNumber(number);
        Message msg = new Message();
        if (byNumber == null) {
            msg.setStatus(-1);
            msg.setResult("问诊号不存在");
        } else {

            msg.setData(byNumber);
            msg.setStatus(0);

        }


        String s = JsonUtil.toJson(msg);
        return s;
    }

    @ResponseBody("/express/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response) {


        int id = Integer.parseInt(request.getParameter("id"));
        String number = request.getParameter("number");
        String userphone = request.getParameter("userphone");
        String username = request.getParameter("username");
        String company = request.getParameter("company");
        int status = Integer.parseInt(request.getParameter("status"));

        Express newExpress = new Express();
        newExpress.setNumber(number);
        newExpress.setCompany(company);
        newExpress.setStatus(status);
        newExpress.setUsername(username);
        newExpress.setUserphone(userphone);
        boolean update = ExpressService.update(id, newExpress);
        Message msg = new Message();
        if (!update) {
            msg.setStatus(-1);
            msg.setResult("更新失败");
        } else {
            msg.setResult("更新成功");

            msg.setStatus(0);

        }

        String s = JsonUtil.toJson(msg);
        return s;

    }

    @ResponseBody("/express/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        boolean delete = ExpressService.delete(id);
        Message msg = new Message();
        if (!delete) {
            msg.setStatus(-1);
            msg.setResult("删除失败");
        } else {
            msg.setResult("删除成功");

            msg.setStatus(0);

        }
        String s = JsonUtil.toJson(msg);
        return s;

    }
}
